package com.wude.guild;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.guild.app.GuildApplication;
import com.wude.guild.servercore.mints.MintsBaseActivity;
import com.wude.http.HttpCom;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

@SuppressLint("HandlerLeak")
public class WelcomeActivity extends MintsBaseActivity {
    private ImageView tu;
//    public String string;

    //请求权限列表
    private String[] storagePermission = new String[]{
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.CAMERA};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        setContentView(R.layout.activity_welcome);

//        FileUtils.verifyStoragePermissions(this);   //动态请求读写权限
        tu = findViewById(R.id.kj);

//        if (BuildConfig.DEBUG) {
//            HttpCom.setURL("http://nc.wudegame.com/app.php?s=");
//        } else {
//            HttpCom.setURL("http://app.happysao.com/app.php?s=");
//        }
        HttpCom.POST(handler, HttpCom.getShanPingURL(), null, false); //请求启动页面图片的请求
        HttpCom.POST(isPayHandler, HttpCom.getIsPay(), null, false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    try {
                        Log.e("请求启动页面图片返回的数据：", msg.obj.toString());
                        JSONObject jsonObject = new JSONObject((String) msg.obj);
                        String url = jsonObject.getString("data");
                        Utils.Fill(tu, url);

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            PermissionsUtil.requestPermission(WelcomeActivity.this, new PermissionListener() {
                                @Override
                                public void permissionGranted(@NonNull String[] permissions) {  //已经获得授权
                                    next();  //跳转页面
                                }

                                @Override
                                public void permissionDenied(@NonNull String[] permissions) {  //没有获得授权
//                                    Utils.TS("未获得读写授权");
                                    finish();
                                }
                            }, storagePermission);
                        } else {
                            next();  //跳转页面
                        }
                    } catch (Exception e) {
                        Log.e("启动页面请求图片异常，", "异常原因+" + e);
                        e.printStackTrace();
                        next();
                    }
                    break;
                case 2:
                    Log.e("启动页面", "handle错误+++++++" + msg.obj);
                    next();
                    break;
            }
        }
    };

    private void next() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                finish();
            }
        };
        timer.schedule(task, 1000 * 2);
    }

    Handler isPayHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1://请求成功  设置标记
                    try {
                        JSONObject object = new JSONObject(msg.obj.toString());
                        int code = object.getInt("code");
                        if (code == 0) {//隐藏支付
                            GuildApplication.application.setPay(false);
//                            GuildApplication.application.setPay(true);
                        } else if (code == 1) {//开启支付
                            GuildApplication.application.setPay(true);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    break;
            }
        }
    };

    /**
     * @param view 测试
     */
    public void test(View view) {
        HttpCom.setURL("http://nc.wudegame.com/app.php?s=");
        debugJump();
    }

    /**
     * @param view 正式
     */
    public void official(View view) {
        HttpCom.setURL("http://app.happysao.com/app.php?s=");
        debugJump();
    }

    private void debugJump() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PermissionsUtil.requestPermission(WelcomeActivity.this, new PermissionListener() {
                @Override
                public void permissionGranted(@NonNull String[] permissions) {  //已经获得授权
                    startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                    finish();  //跳转页面
                }

                @Override
                public void permissionDenied(@NonNull String[] permissions) {  //没有获得授权
//                                    Utils.TS("未获得读写授权");
                    finish();
                }
            }, storagePermission);
        }
    }
}